package demo_test_selenium.demo_test_selenium.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpFileDemo {

    public static void main(String[] args) throws Exception {
        String dirPath = "F:\\IdeaProjects\\demo_test_selenium\\tmp";
        HashMap<String, String> id2Name = new HashMap<>();
        List<Map<String, String>> maps = ExcelDataUtil.redExcel("F:\\IdeaProjects\\demo_test_selenium\\tmp\\取数 - 终.xlsx");
        for (Map<String, String> map : maps) {
            id2Name.put(map.get("合同号"), map.get("案件唯一标识码"));
        }

        File orgDir = new File(dirPath + "\\20210924");
        File imgDir = new File(dirPath + "\\img");
        File pdfDir = new File(dirPath + "\\pdf");
        File[] files = orgDir.listFiles();
        for (File file : files) {
            if (!id2Name.containsKey(file.getName().replaceAll(".pdf",""))){
                continue;
            }
            String oriFilePath = file.getAbsolutePath();
            String imgFilePath = (imgDir + "\\" + file.getName()).replaceAll(".pdf", ".png");
            String pdfFilePath = (pdfDir + "\\" + id2Name.get(file.getName().replaceAll(".pdf","")) + ".pdf");
            File file1 = new File(pdfFilePath);
            if (!file1.exists()) {
                eSingScreenshot(oriFilePath, imgFilePath, pdfFilePath);
            }
        }
    }

    private static void eSingScreenshot(String oriFilePath, String imgFilePath, String pdfFilePath) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "F:\\IdeaProjects\\driver\\chromedriver.exe");

        // 1.关闭提示框，新增一个预操作对象
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        options.setExperimentalOption("useAutomationExtension", false);
        options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));

        // 1.创建webdriver驱动，将预操作对象加载到浏览器中
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();

        // 2.打开网页
        driver.get("https://www.esign.cn/pcgw/cp/htyz/");

        // 3.执行操作
        driver.findElement(By.id("filea")).sendKeys(oriFilePath);
        Thread.sleep(2000);

        // 4.调整窗口
        ((ChromeDriver) driver).executeScript("document.body.style.transform='scale(0.67)'");
//    ((ChromeDriver) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
        ((ChromeDriver) driver).executeScript("window.scrollTo(0,400)");
        Thread.sleep(1000);

        // 5.截图
        String imgName = imgFilePath;
        try {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //获取全屏幕的宽高尺寸等数据
            // *** 核心代码 *** 拷贝屏幕到一个BufferedImage对象screenshot
            BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
            File f = new File(imgName);
            System.out.print("Save File " + imgName);
            // 将screenshot对象写入图像文件
            ImageIO.write(screenshot, "png", f);
            System.out.print("..Finished!\n");
        } catch (Exception ex) {
            System.out.println(ex);
        }

        // 6.图片转pdf
        ImgToPdfUtil img = new ImgToPdfUtil();
        img.imgOfPdf(pdfFilePath, imgName);

        driver.quit();
    }
}
